package geo.com.geosdkdemo.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Objects;

import geo.com.geosdk.GeoManage;
import geo.com.geosdk.listener.ReceiverDataListener;
import geo.com.geosdkdemo.view.adapter.DebugCommandRecycleviewAdapter;
import geo.com.geosdkdemo.R;

public class DebugSetupActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private RecyclerView recyclerView;
    private DebugCommandRecycleviewAdapter adapter;
    private ArrayList<String> list;

    private MyHandler myHandler;


    //负责更新UI
    private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            String receiveData= (String) msg.obj;
            list.add(receiveData);
            adapter.notifyDataSetChanged();
            recyclerView.smoothScrollToPosition(adapter.getItemCount()-1);

        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debug_setup);
        initView();
        initData();
    }

    private void initData() {
        tvCommonTitle.setText("串口调试");
        myHandler=new MyHandler();
        list=new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new DebugCommandRecycleviewAdapter(list);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(Objects.requireNonNull(getApplication().getDrawable(R.drawable.shape_view)));
        recyclerView.addItemDecoration(dividerItemDecoration);
        recyclerView.setAdapter(adapter);

        //2.原始数据回调
        GeoManage.getInstance().setReceiverDataListener(new ReceiverDataListener() {
            @Override
            public void OnReceiverCallBack(int nLength, byte[] data) {
                Log.d("Debug-OnReceiverCall", new String(data,0,nLength));
                //显示接收到的所有数据
                String receiveData=new String(data,0,nLength);
                Message msg=new Message();
                msg.obj=receiveData;
                myHandler.sendMessage(msg);

            }
        });
    }

    private void initView() {
        ivCommonBack=findViewById(R.id.iv_common_back);
        tvCommonTitle=findViewById(R.id.tv_common_title);
        recyclerView = (RecyclerView)findViewById( R.id.recycler_view );
        ivCommonBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            finish();
        }
    }
}
